"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebServer = void 0;
const Config_1 = require("./src/config/Config");
const RedisConfig_1 = require("./src/config/RedisConfig");
const Logger_1 = require("./src/engine/utils/Logger");
const RedisManager_1 = require("./src/engine/utils/RedisManager");
const WebReqHandler_1 = require("./src/engine/webserver/WebReqHandler");
const ZoneModel_1 = require("./src/model/ZoneModel");
const IpUtil_1 = require("./src/engine/utils/IpUtil");
const fs = require("fs");
const path = require("path");
const express = require("express");
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const http = require("http");
class WebServer {
    init() {
        Config_1.Config.serverType = Config_1.Config.ServerType_List.WebServer;
        Logger_1.Logger.tag = Config_1.Config.ServerType_List.WebServer;
        IpUtil_1.IpUtil.loadFile(path.resolve() + "/ip.txt");
        // ZoneModel.init();
        // RedisManager.instance.init();
        for (let i = 0; i < Config_1.Config.WebPortList.length; i++) {
            let app = express();
            let publicPath = path.resolve() + "/public" + Config_1.Config.WebPortList[i];
            if (!fs.existsSync(publicPath)) {
                fs.mkdirSync(publicPath);
            }
            app.use(bodyParser.urlencoded({ extended: false })); //解析post请求数据
            app.use(bodyParser.text()); // for parsing application/json
            app.use(bodyParser.json()); // for parsing application/json
            //设置静态网页目录
            let options = {
                setHeaders: function (res, path, stat) {
                    res.set('Access-Control-Allow-Origin', '*');
                }
            };
            app.use(express.static(publicPath, options));
            app.use(cookieParser());
            app.all('*', function (req, res, next) {
                req.header("Content-Type", "application/json;charset=UTF-8");
                res.header("Access-Control-Allow-Origin", "*"); //项目上线后改成页面的地址
                res.header("Content-Type", "application/json;charset=UTF-8");
                res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
                next();
            });
            WebReqHandler_1.WebReqHandler.instance.init(app);
            http.createServer(app).listen(Config_1.Config.WebPortList[i]);
            Logger_1.Logger.info("startWebServer suc=", Config_1.Config.WebPortList[i]);
        }
    }
    initServerList() {
        let redis = RedisManager_1.RedisManager.instance.getRedis(RedisConfig_1.RedisConfig.Database_Index.Users);
        let str = JSON.stringify(ZoneModel_1.ZoneModel.zoneModelList);
        // Logger.log("initServerLis==", ZoneModel.zoneModelList, str)
        redis.setValue(ZoneModel_1.ZoneModel.Redis_Key, str);
    }
}
exports.WebServer = WebServer;
WebServer.instance = new WebServer();
try {
    WebServer.instance.init();
    Logger_1.Logger.log("start suc====");
}
catch (error) {
    Logger_1.Logger.error("WebMain.error=", error);
}
